<?php

namespace app\admin\controller;


use app\admin\model\Company;
use cmf\controller\AdminBaseController;
use think\facade\Db;


class CompanyController extends AdminBaseController
{
    /**
     *列表
     * @adminMenu(
     *     'name'   => '供应商维护',
     *     'parent' => 'admin/basicdata/de',
     *     'display'=> true,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '',
     *     'param'  => ''
     * )
     */
    public function index(){
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = Company::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');

        $this->assign('user_arr', $user_arr);

        $companytypea_arr = array(1 => '自主', 2 => '来料',3=>'委外');
        $this->assign('companytypea_arr', $companytypea_arr);

        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);

        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(),'domain'=>request()->domain().'/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '新增',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function add(){
        $companytypea_arr=array(0=>array('id'=>1,'name'=>'自主'),1=>array('id'=>2,'name'=>'来料'), 2 => array('id' => 3, 'name' => '委外'));
        $this->assign('companytypea_arr', $companytypea_arr);
        return $this->fetch('',['domain'=>request()->domain().'/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '新增提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function addPost(){
        $params = array_map(function($v){
            return is_array($v)?implode(',',$v):$v;
        },input('post.'));

        if(!$params){
            $this->error('非法参数');
        }
        $params['add_time'] = time();
        $params['update_time'] = time();
        $params['admin_id']=cmf_get_current_admin_id();
//        Company::create($params);
        $id= Db::name('company')->insertGetId($params);
        $params['id']=$id;
        cmf_install_log($params,'company_log','update');
        $this->success('操作成功', 'index');
    }
    /**
     *  @adminMenu(
     *     'name'   => '修改',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function edit(){
        $id = input('get.id/d');

        if(!is_numeric($id)){
            $this->error('非法参数');
        }
        $companytypea_arr=array(0=>array('id'=>1,'name'=>'自主'),1=>array('id'=>2,'name'=>'来料'), 2 => array('id' => 3, 'name' => '委外'));
        $this->assign('companytypea_arr', $companytypea_arr);
        $res = Company::where('id',$id)->find();


        return $this->fetch('',['vo'=>$res,'domain'=>request()->domain().'/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '修改提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function editPost(){
        $params = array_map(function($v){
            return is_array($v)?implode(',',$v):$v;
        },input('post.'));

        if(!$params || empty($params['id'])){
            $this->error('非法参数');
        }
        $params['update_time'] = time();
        $params['admin_id']=cmf_get_current_admin_id();
        Company::update($params);
        cmf_install_log($params,'company_log','update');
        $this->success('操作成功', 'index');
    }
    /**
     *  @adminMenu(
     *     'name'   => '删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function del(){
        $id = input('get.id/d');

        if(!(is_numeric($id) && $id>0)){
            $this->error('非法参数');
        }

        Company::where('id',$id)->update(array('delete_time' => time(),'admin_id'=>cmf_get_current_admin_id()));
        $params= Db::name('company')->where('id',$id)->find();
        cmf_install_log($params,'company_log','del');
        $this->success('删除成功');
        $this->success('删除成功');
    }

    public function field(){
        $id = input('get.id/d');
        $field = input('get.field');

        if(!(is_numeric($id) && $id>0 && $field)){
            $this->error('非法参数');
        }

        $info = Company::field($field)->where('id',$id)->find();

        $this->success('ok','',$info[$field]);
    }
}